// Debug stuff
var debug = null;
var debug2 = null;
var debug2 = null;
var iBallPosX = 1;
var iBallPosY = 1;
var iBallPosX2 = 1;
var iBallPosY2 = 1;
var directionX = 1;
var directionY = 1;
var dummySprite = null;
var count = 0;

// Graphics
var canvas = null;
var ctx = null;

// Keys
var leftKey = Kb_trapkey("LEFT");
var rightKey = Kb_trapkey("RIGHT");
var thrustKey = Kb_trapkey("z");

// Global state management
var alreadyHooked = false;

// The Ship
var theShip = new ship();

// Game states
// 0 = demo
// 10 = setup
// 11 = ship exploded
// 12 = play
// 13 = end
var gState = 0;

// Time managment
var elapsed;
var preElapsed;

var MPF = 33;		// milliseconds per frame. 17 ~= 60fps. 33 ~= 30fps

// Notes: rgba(r,g,b,a)

// **********************************************************
// ** Framework Functions                                  **
// **********************************************************
function calibrationComplete()
{
	if (!alreadyHooked)
	{
		alreadyHooked = true;
		Gl_hook("draw()");	// This should only be called once.
	}
	
	Sp_linuxcompatible = true;
	dummySprite = new Sp_Sprite();
	dummySprite.setImage("http://examplegadgets.googlecode.com/svn/trunk/penquin.png", 16, 16, 1, 1);
	dummySprite.switchOff();
	
	ctx.strokeRect(0, 0, 200, 100);

	Gl_start();
}

function spout_resume()
{
	elapsed = new Date();
	preElapsed = new Date();
	Gl_start();
}

function spout_start()
{
	debug = document.getElementById("debug");
	debug2 = document.getElementById("debug2");
	debug3 = document.getElementById("debug3");
	
	//Gl_calibrate("calibrationComplete()", 16);
   canvas = document.getElementById("canvas");
   if (canvas.getContext)
   {
		ctx = canvas.getContext("2d");
	}
	else
	{
		alert("Could not get context");
	}
	
	elapsed = new Date();
	preElapsed = new Date();
	Gl_setTimerSpeed(MPF);
	calibrationComplete();
}

function spout_stop()
{
	elapsed = new Date();
	preElapsed = new Date();
	Gl_stop();
}

function spout_reset()
{
	theShip.reset();
}

// **********************************************************
// ** Game Functions                                       **
// **********************************************************
// Use this function if you want the ship's thrust
// to effect the direction only when thrust is being applied.

function drawStuff()
{
   // debug draw cursors
   //iBallPosX += directionX;
   //if (iBallPosX > 194)
   //  	directionX = -1;
   //if (iBallPosX < 2)
   //  	directionX = 1;
   //ctx.fillStyle = "rgb(200,0,200)";
   //ctx.fillRect (iBallPosX, iBallPosY, 8, 8);

   iBallPosY2 += directionY;
   if (iBallPosY2 > 90)
     	directionY = -1;
   if (iBallPosY2 < 2)
   	directionY = 1;
   ctx.fillStyle = "rgb(0,200,200)";
   ctx.fillRect(iBallPosX2, iBallPosY2, 4, 4);
   
}

function drawStuff2()
{
   // debug draw cursors
   var c1 = "rgb(200,200,200)";
   var c2 = "rgb(100,100,100)";
   var c3 = "rgb(200,0,200)";
   var c4 = "rgb(200,200,0)";
   var rb = false;
   var cb = false;
	var cr = 1;
	var cc = 1;
   for (var c = 0; c < 24; c++)
   {
  		rb = !rb;
   	for (var r = 0; r < 12/2; r++)
   	{
   		if (rb)
   		{
   			ctx.fillStyle = c1;
   		}
   		else
   		{
   			ctx.fillStyle = c2;
   		}
  			rb = !rb;
  			ctx.fillRect (cc, cr, 8, 8);
  			cr += 8;
   	}
   	cr = 1;
		cc += 8;
   }
}

function draw() {
	elapsed = new Date();
	
	// Clear the canvas. Note clear only inside.
	// Leave the border untouched. This saves a stroke.
	ctx.fillStyle = "rgb(255,255,255)";
	ctx.fillRect (1, 1, 192, 96);
	
	drawStuff2();
	for (var c = 0; c < 50; c += 1)
		drawStuff();
	
	for (var x = 0; x < 5000; x++)
	{
		count++;
		if (count < 100)
			count = 30;
	}
	// Calc new ship position first.
	theShip.update(MPF);
	theShip.draw();

	// Clean the edges
	ctx.strokeStyle = "rgb(0,0,0)";
	ctx.strokeRect(0, 0, 194, 98);
	
	preElapsed = new Date();
	var dt = preElapsed.getMilliseconds() - elapsed.getMilliseconds();
	debug.innerHTML = dt;
	
}

// **********************************************************
// ** Misc code. Can be removed                            **
// **********************************************************
